package com.kzy.demoadmin.web.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kzy.demoadmin.web.entity.Book;
import com.kzy.demoadmin.web.entity.BookPageQuery;
import com.kzy.demoadmin.web.exception.BizException;
import com.kzy.demoadmin.web.mapper.BookMapper;
import com.kzy.demoadmin.web.service.BookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class BookServiceImpl implements BookService {

    @Autowired
    private BookMapper bookMapper;

    /**
     * 根据id查询
     * @param integer
     * @return
     */
    @Override
    public Book findById(Integer integer) {
        return bookMapper.findById(integer);
    }

    /**
     * 分页查询
     * @param query
     * @return
     */
    @Override
    public PageInfo<Book> getBookPage(BookPageQuery query) {
        PageHelper.startPage(query.getPageNo(), query.getPageSize());
        List<Book> list = bookMapper.searchBy(query);
        System.out.println(list);
        return new PageInfo<>(list);
    }

    /**
     * 删除书籍
     * @param id
     * @return
     */
    @Override
    public int deleteById(Integer id) {

        return bookMapper.deleteById(id);
    }

    /**
     * 新增书籍
     * @param book
     */
    @Override
    public void add(Book book) {
        //1、图书名称不能为空
        if (book.getName() == null || "".equals(book.getName())){
            throw new BizException("图书名称不能为空");
        }

        //2、图书作者不能为空
        if (book.getAuthor() == null || "".equals(book.getAuthor())){
            throw new BizException("图书作者不能为空");
        }

        //3、图书价格不能为空
        if (book.getPrice() == null || "".equals(book.getPrice())){
            throw new BizException("图书价格不能为空");
        }

        //4、图书出版社不能为空
        if (book.getPress() == null || "".equals(book.getPress())){
            throw new BizException("图书出版社不能为空");
        }

        //2、判断状态是否有值
        if (book.getStatus() == null){
            book.setStatus(1);
        }

        //给新增书籍一个默认的数量
        book.setNum(20);

        bookMapper.insert(book);
    }

    /**
     * 修改书籍
     * @param book
     */
    @Override
    public void edit(Book book) {
        //1、图书名称不能为空
        if (book.getName() == null || "".equals(book.getName())){
            throw new BizException("图书名称不能为空");
        }

        //2、图书作者不能为空
        if (book.getAuthor() == null || "".equals(book.getAuthor())){
            throw new BizException("图书作者不能为空");
        }

        //3、图书价格不能为空
        if (book.getPrice() == null || "".equals(book.getPrice())){
            throw new BizException("图书价格不能为空");
        }

        //4、图书出版社不能为空
        if (book.getPress() == null || "".equals(book.getPress())){
            throw new BizException("图书出版社不能为空");
        }

        //5、修改书籍
        bookMapper.update(book);
    }

    @Override
    public List<Book> getAll() {
        return  bookMapper.getAll();
    }

}
